package src.io.bio;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;


public class Server2 {
   /* public static void main(String[] args) throws IOException {
        ServerSocket serverSocke= new ServerSocket(9090);
        Socket socket = serverSocke.accept();
        InputStream inputStream=socket.getInputStream();
        byte [] bytes =new byte[1024];
        while (inputStream.read()!=-1){
            inputStream.read(bytes);
            System.out.println(new String(bytes));
        }
        OutputStream outputStream=socket.getOutputStream();
        outputStream.write("我收到了你的请求".getBytes("utf-8"));
    }*/

    /*public static void main(String[] args) {
        ServerSocket serverSocket;
        InputStreamReader in=null;
        OutputStreamWriter out=null;

        {
            try {
                serverSocket = new ServerSocket(9999);
                System.out.println("开始营业");
                Socket socket = serverSocket.accept();
                System.out.println("有一个小毛孩来了");
                in = new InputStreamReader(socket.getInputStream());
                out = new OutputStreamWriter(socket.getOutputStream());
                out.write("你好小小客户端");
                out.flush();
                char [] c = new char[100];
                while (in.read() != -1){
                    System.out.println(c);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (out!=null){
                    try {
                        out.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (in!=null){
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }*/

    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket=null;
        BufferedReader in=null;
        PrintWriter out=null;
        try {
            serverSocket=new ServerSocket(8888);

            System.out.println("开始");
            Socket socket =serverSocket.accept();

            in =new BufferedReader(new InputStreamReader(socket.getInputStream()));
             out = new PrintWriter(socket.getOutputStream(),true);

            /*while (in.read()!=-1){
                if (in.readLine()==null){
                    break;
                }
                System.out.println(in.readLine());

                out.println("小毛孩你好，我是服务器");
            }*/
            System.out.println("---接收到客户端:---"+in.readLine());
            Thread.sleep(10000L);
            out.println("----发送给客户端---小毛孩你好，我是服务器");
            Thread.sleep(3000L);
           /* int a=200;
            while (a>100){
                Thread.sleep(100L);
                out.println("服务器你好，我是客户");
                a--;
            }


            while (a>10){
                Thread.sleep(100L);
                System.out.println(in.readLine());
                a--;
            }*/
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (out!=null){
                out.close();
            }
            if (in!=null){
                in.close();
            }
        }
    }

}
